﻿@{
    ViewBag.Title = "Product";
    Layout = "~/Views/Shared/_ViewFormPage.cshtml";
}
@model ProductVM
@section head{

    <script charset="utf-8" src="/Scripts/kindeditor-4.1.3/kindeditor-min.js?@CommonSetting.JsVersion"></script>
    <script charset="utf-8" src="/Scripts/kindeditor-4.1.3/lang/zh_CN.js?@CommonSetting.JsVersion"></script>
    <script src="/Scripts/AjaxFileUploaderV2.1/ajaxfileupload.js"></script>
    <script type="text/javascript">
        var editor;
        KindEditor.ready(function (K) {
            editor = K.create('textarea[name="Description"]', {
                resizeType: 1,
                allowPreviewEmoticons: false,
                allowImageUpload: true,
                allowFileManager: false,
                syncType: 'form',
                width: '800px',
                height: '240px',
                uploadJson: '@Url.Action("Upload", "File")'
            });
        });

        $(function () {
            $("#btnSave").one("click",function() {
                saveProduct();
            });


            $("#btnUpload").click(function (e) {
                e.preventDefault();
                ajaxFileUpload();
            });

            $("#partitionFloor").change(function () {
                loadPartition($("#partitionParent"), $(this).val());
            });

            $("#partitionParent").change(function () {
                loadPartition($("#PartitionId"), $(this).val());
            });

        });


        function ajaxFileUpload() {
            var dialog = $.ligerDialog.waitting('正在上传,请稍候...');
            $.ajaxFileUpload({
                url: '/File/Upload',
                secureuri: false,
                fileElementId: 'imgFile',
                dataType: 'json',
                cache: false,
                data: { dir: 'image', localUrl: '' },
                success: function (data, status) {
                    if (typeof (data.error) !== 'undefined') {
                        if (data.error === 0) {
                            $("#productImage").attr("src", data.url);
                            $("#ImgUrl").val(data.url);
                        } else {
                            $.ligerDialog.error('上传出错:' + data.message);
                        }
                    }
                },
                error: function (data, status, e) {
                    $.ligerDialog.error('上传出错:' + e);
                }
                ,
                complete: function (XMLHttpRequest, textStatus) {
                    dialog.close();
                }
            })
            return false;
        }


        function loadPartition(target, val) {
            $(target).empty().append($("<option />").attr("value", "").text("请选择......"));

            var $v = val;

            $.ajax({
                url: '/Product/GetPartitions',
                type: 'POST',
                data: { parentId: $v },
                success: function (data, textStatus, jqXHR) {
                    //alert(textStatus);
                    if (textStatus == "success") {
                        var len = data.length;
                        for (var i = 0; i < len; i++) {
                            var $option = $("<option />").attr("value", data[i].PartitionId).text(data[i].Name)
                            $(target).append($option);
                        }
                    }

                },
                error: function (e, r, p) {
                    // alert('');
                }
            });
        }


        function saveProduct() {
            if ($("form").valid()) {
                editor.sync("Description");
                $('form').ajaxSubmit({
                    type:"Post",
                    success: function (callback) {
                        if (callback) {
                            $.ligerDialog.success('保存成功', function (yes) {
                                self.location.href = "/Product";
                            });

                            //$.messageTip().show("保存成功！");
                        }
                        else {
                            $.ligerDialog.error('保存失败');
                            $("#btnSave").one("click", function () {
                                saveProduct();
                            });
                        }
                    },
                    error: function (ex) {
                        $.ligerDialog.error('保存失败');
                        $("#btnSave").one("click", function () {
                            saveProduct();
                        });
                    }

                });
            }
        }

    </script>
    @if (false)
    {
        <script src="../../Scripts/jquery-1.8.2.js"></script>
    }
    <style type="text/css">
    </style>
}
<div id="header" class="header">
    <h4>产品管理 -- 添加</h4>
</div>
@using (Html.BeginForm("Product", "Product", FormMethod.Post))
{
    @Html.HiddenFor(model => model.CompanyId)
    @Html.HiddenFor(model => model.Id)
    <div id="detail_forms">
        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                产品名称
            </div>
            <div class="formItem textItem">
                @Html.TextBoxFor(model => model.ProductName, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
            <div class="formItem labelItem">
                厂家(产地)
            </div>
            <div class="formItem textItem">
                @Html.TextBoxFor(model => model.Manufacturers, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
        </div>

        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                型号
            </div>
            <div class="formItem textItem">
                @Html.TextBoxFor(model => model.Model, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
            <div class="formItem labelItem">
                关键字
            </div>
            <div class="formItem textItem">
                @Html.TextBoxFor(model => model.KeyWords, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
        </div>

        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                类别
            </div>
            <div class="formItem textItem">
                @Html.DropDownList("partitionFloor", Model.Floors)
                @Html.DropDownList("partitionParent", Model.Parents)
                @Html.DropDownListFor(model => model.PartitionId, Model.Children)
            </div>
            <div class="formItem msgItem">
            </div>
        </div>



        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                用途
            </div>
            <div class="formItem textItem">
                @Html.TextAreaFor(model => model.Purpose, new { @class = "ifunny-textareas" })
            </div>
            <div class="formItem msgItem">
            </div>
        </div>

        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                价格
            </div>
            <div class="formItem textItem">
                @Html.TextBoxFor(model => model.Price, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
            <div class="formItem labelItem">
                价格单位
            </div>
            <div class="formItem textItem">
                @Html.TextBoxFor(model => model.PriceUnit, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
        </div>

        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                图片
            </div>
            <div class="formItem textItem">
                <table border="0">
                    <tr>
                        <td style="height:40px;">
                            <input type="file" id="imgFile" name="imgFile" />
                            <span class="buttons">
                                <a href="javascript:void(0);" id="btnUpload">上传</a>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div id="showImage">
                                <img src='@Model.Image'  style="height:120px; cursor:pointer; width:200px;" alt="" id="productImage" />
                            </div>
                        </td>
                        <td style="vertical-align: bottom;">
                            <div style="width: 220px; margin-left: 10px; display: block">
                                @Html.HiddenFor(m => m.Image)
                            </div>
                        </td>
                    </tr>
                </table>
                @Html.HiddenFor(model => model.Image)
            </div>
            <div class="formItem msgItem">
            </div>
        </div>

        <div class="formsItems clearfix">
            <div class="formItem labelItem">
                描述
            </div>
            <div class="formItem textItem">
                @Html.TextAreaFor(model => model.Description, new { @class = "texts" })
            </div>
            <div class="formItem msgItem">
            </div>
        </div>



        <div id="btnContainer" class="formsItems">
            <div class="formItem labelItem">
                &nbsp;
            </div>
            <div class="formItem textItem">
                <a href="javascript:void(0);" id="btnSave">保存</a>
                <a href="@Url.Action("Index")" id="btnCancel">取消</a>
            </div>
            <div class="formItem">
            </div>
        </div>
        <div class="formsItems"></div>
    </div>
}
